package com.pc.scrs.service.impl;

import com.pc.scrs.bean.po.Teacher;
import com.pc.scrs.bean.vo.EmailVo;
import com.pc.scrs.bean.vo.MobileVo;
import com.pc.scrs.bean.vo.TeacherVo;
import com.pc.scrs.bean.vo.listVo.TeacherListVo;
import com.pc.scrs.bean.vo.pageInfo.TeacherListVoPageInfo;
import com.pc.scrs.dao.TeacherDao;
import com.pc.scrs.service.ITeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class TeacherService implements ITeacherService {

    @Resource
    private TeacherDao teacherDao;

    @Override
    public List<MobileVo> mobileList() {
        List<String> mobileList = teacherDao.queryMobileList();
        List<MobileVo> mobileVoList = new ArrayList<>();
        for (String mobile : mobileList) {
            MobileVo mobileVo = new MobileVo();
            mobileVo.setMobile(mobile);
            mobileVoList.add(mobileVo);
        }
        return mobileVoList;
    }

    @Override
    public List<EmailVo> emailList() {
        List<String> emailList = teacherDao.queryEmailList();
        List<EmailVo> emailVoList = new ArrayList<>();
        for (String email : emailList) {
            EmailVo emailVo = new EmailVo();
            emailVo.setEmail(email);
            emailVoList.add(emailVo);
        }
        return emailVoList;
    }

    @Override
    public TeacherListVoPageInfo teacherList(String mobile, String email, Integer enable, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Teacher> teacherList = teacherDao.queryTeacherList(mobile, email, enable);
        List<TeacherListVo> teacherListVoList = new ArrayList<>();
        PageInfo<Teacher> teacherPageInfo = new PageInfo<>(teacherList);
        for(Teacher teacher : teacherList){
            TeacherListVo teacherListVo = new TeacherListVo();
            teacherListVo.setId(teacher.getId());
            teacherListVo.setEmail(teacher.getEmail());
            teacherListVo.setMobile(teacher.getMobile());
            teacherListVo.setName(teacher.getName());
            teacherListVo.setEnable(teacher.getEnable());
            teacherListVo.setCreateTime(teacher.getCreateTime());
            teacherListVoList.add(teacherListVo);
        }
        TeacherListVoPageInfo teacherListVoPageInfo = new TeacherListVoPageInfo();
        teacherListVoPageInfo.setTeacherListVoList(teacherListVoList);
        teacherListVoPageInfo.setPageNum(teacherPageInfo.getPageNum());
        teacherListVoPageInfo.setPageSize(teacherPageInfo.getPageSize());
        teacherListVoPageInfo.setPages(teacherPageInfo.getPages());
        teacherListVoPageInfo.setTotal(teacherPageInfo.getTotal());
        return teacherListVoPageInfo;
    }

    @Override
    public Integer disableTeacher(Integer id) {
        Integer result = teacherDao.disableTeacher(id);
        return result;
    }

    @Override
    public Integer enableTeacher(Integer id) {
        Integer result = teacherDao.enableTeacher(id);
        return result;
    }

    @Override
    public Integer updatePassword(Integer id, String password) {
        Integer result = teacherDao.updatePassword(id, password);
        return result;
    }

    @Override
    public TeacherVo queryTeacherById(Integer id) {
        Teacher teacher = teacherDao.queryTeacherById(id);
        TeacherVo teacherVo = new TeacherVo();
        teacherVo.setId(teacher.getId());
        teacherVo.setName(teacher.getName());
        teacherVo.setEmail(teacher.getEmail());
        teacherVo.setAvatar(teacher.getAvatar());
        teacherVo.setMobile(teacher.getMobile());
        teacherVo.setAvatar(teacher.getAvatar());
        teacherVo.setStatus(teacher.getStatus());
        teacherVo.setIntro(teacher.getIntro());
        return teacherVo;
    }

    @Override
    public Integer updateTeacher(TeacherVo teacherVo) {
        Teacher teacher = new Teacher();
        teacher.setId(teacherVo.getId());
        teacher.setEmail(teacherVo.getEmail());
        teacher.setMobile(teacherVo.getMobile());
        teacher.setName(teacherVo.getName());
        teacher.setAvatar(teacherVo.getAvatar());
        teacher.setIntro(teacherVo.getIntro());
        int result = teacherDao.updateByPrimaryKeySelective(teacher);
        return result;
    }

    @Override
    public Integer addTeacher(TeacherVo teacherVo) {
        Teacher teacher = new Teacher();
        teacher.setId(teacherVo.getId());
        teacher.setEmail(teacherVo.getEmail());
        teacher.setPassword(teacherVo.getPassword());
        teacher.setMobile(teacherVo.getMobile());
        teacher.setName(teacherVo.getName());
        teacher.setAvatar(teacherVo.getAvatar());
        teacher.setIntro(teacherVo.getIntro());
        teacher.setStatus(teacherVo.getStatus());
        int result = teacherDao.insertSelective(teacher);
        return result;
    }
}
